package o11maxarray;

/**
 * @author shkstart
 * @create 2022-01-06-18:53
 */
public class MaxArray {

    public static void main(String[] args) {
        int[] nums = {-1,0,-2};
        int num = maxSubArray(nums);
        System.out.println(num);
    }

    public static int maxSubArray(int[] nums) {
        int [] dp = new int[nums.length];
        int res  = nums[0];
        dp[0] = nums[0];
        for (int i = 1; i < nums.length; i++) {
            dp[i] = Math.max(dp[i-1]+nums[i],nums[i]);
            res = Math.max(res,dp[i]);
        }
        return res;
    }

    public static int maxSubArrayOptim(int[] nums) {
        int pre;
        int res  = nums[0];
        pre = nums[0];
        for (int i = 1; i < nums.length; i++) {
            pre = Math.max(pre+nums[i],nums[i]);
            res = Math.max(res,pre);
        }
        return res;
    }

}
